package com.mybatis.imook;

import com.mybatis.dto.GoodsDTO;
import com.mybatis.entity.Goods;
import com.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisTestor {
    @Test
    public void testSqlSessionFactory() throws IOException {
        //利用Reader加载 classpath下的mybatis-config.xml核心配置文件
       Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
       //初始化SqlSessionFactory对象，同时解析mybatis-config.xml文件
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        System.out.println("加载成功");
        //创建SqlSession对象，SqlSession是JDBC的扩展类，用于与数据库交互
        SqlSession sqlSession = null;
       try {
           sqlSession = sqlSessionFactory.openSession();
           Connection connection= sqlSession.getConnection();
           System.out.println(connection);
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           if (sqlSession != null) {
               //如果type="POOLED"，代表使用连接池，close则是将连接回收到连接池中
               //如果type="UNPOOLED"，代表直连，close则会调用Connection.close()方法关闭
               sqlSession.close();
           }
       }
    }
    @Test
    public void testMyBatisUtils() throws Exception {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtils.openSession();
//            sqlSession.delete()
            Connection connection = sqlSession.getConnection();
            System.out.println(connection);
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testSelectAll() throws Exception {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtils.openSession();
            List<Goods> list = sqlSession.selectList("goods.selectAll");
            for (Goods good : list) {
                System.out.println(good.getSubTitle());
            }
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testSelectOne() throws Exception {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtils.openSession();
           Goods good = sqlSession.selectOne("goods.selectById",1);
            System.out.println(good.getSubTitle());
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testSelectByPriceRange() throws Exception {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtils.openSession();
            Map param = new HashMap();
            param.put("min",3);
            param.put("max",6);
            param.put("limt",10);
            List<Goods> goods = sqlSession.selectList("goods.selectByPriceRange",param);
            for (Goods good : goods) {
                System.out.println(good.getSubTitle());
            }
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testSelectGoodsDTO() throws Exception {
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtils.openSession();
            List<GoodsDTO> list = sqlSession.selectList("goods.selectGoodsDTO");
            for (GoodsDTO goodsDTO : list) {
                System.out.println(goodsDTO.getCategory().getCategoryName());
            }
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }

    @Test
    public void testInsert() throws Exception {
        SqlSession sqlSession = MyBatisUtils.openSession();
       try {
           Goods goods = new Goods();
           goods.setCategoryId(2);
           goods.setDiscount(0.98f);
           goods.setPrice(59.9f);
           goods.setTitle("衣服男士2");
           goods.setSubTitle("卫衣2");
           goods.setIsFreeDelivery(1);
           int num =  sqlSession.insert("goods.insert", goods);
           System.out.println(num);
           System.out.println(goods.getId());
           sqlSession.commit(); //提交事务数据
       }catch (Exception e){
          if (sqlSession != null) {
              sqlSession.rollback();  //回滚事务
          }
           throw e;
       }finally {
           MyBatisUtils.closeSession(sqlSession);
       }
    }

    @Test
    public void testUpdate() throws Exception {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
           Goods goods = sqlSession.selectOne("goods.selectById",7);
           goods.setTitle("更新测试商品");
           int num =  sqlSession.update("goods.update", goods);
           sqlSession.commit();
        }catch (Exception e){
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testDelete() throws Exception {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            int num =  sqlSession.delete("goods.delete", 8);
            sqlSession.commit();
        }catch (Exception e){
            if (sqlSession != null) {
                sqlSession.rollback();
            }
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
}
